package com.terabits.smartriver.service.impl;

import com.terabits.smartriver.mapper.BoxMapper;
import com.terabits.smartriver.mapper.BoxMaterialMapper;
import com.terabits.smartriver.mapper.RegionMapper;
import com.terabits.smartriver.meta.po.Box;
import com.terabits.smartriver.meta.po.BoxMaterial;
import com.terabits.smartriver.meta.po.ProjectMaterial;
import com.terabits.smartriver.service.BoxService;
import com.terabits.smartriver.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class BoxServiceImpl extends BaseService implements BoxService {
    @Resource
    private BoxMapper boxMapper;
    @Resource
    private RegionMapper regionMapper;
    @Autowired
    private BoxMaterialMapper boxMaterialMapper;

    @Override
    public List getBox(PageData pageData) {
        return boxMapper.selectBox(pageData);
    }

    @Override
    public int addBox(Box box) {
//        Example example = new Example(Box.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.orEqualTo("boxNum", box.getBoxNum()).orEqualTo("imei", box.getBoxNum());
//        if (!StringUtils.isEmpty(box.getImei())) {
//            criteria.orEqualTo("boxNum", box.getImei()).orEqualTo("imei", box.getImei());
//        }
        List<Box> boxes = boxMapper.selectOneBox(box);

        if (boxes != null && boxes.size() != 0) {
            return -1;
        }
        return boxMapper.insertSelective(box);
    }

    @Override
    public int updateBox(Box box) {
//        if (!StringUtils.isEmpty(box.getImei()) || !StringUtils.isEmpty(box.getBoxNum())) {
//            Example example = new Example(Box.class);
//            Example.Criteria criteria = example.createCriteria();
//            if (!StringUtils.isEmpty(box.getImei())) {
//                criteria.orEqualTo("boxNum", box.getBoxNum()).orEqualTo("imei", box.getBoxNum());
//            }
//            if (!StringUtils.isEmpty(box.getImei())) {
//                criteria.orEqualTo("boxNum", box.getImei()).orEqualTo("imei", box.getImei());
//            }
//            if(!StringUtils.isEmpty(box.getImei())){
//                List<Box> boxes = boxMapper.selectOneBoxByImei(box);
//                if (boxes != null && boxes.size() != 0) {
//                    return -2;
//                }
//            }
//            List<Box> boxes = boxMapper.selectOneBox(box);
//
//            if (boxes != null && boxes.size() != 0) {
//                return -1;
//            }
//        }
        return boxMapper.updateByPrimaryKeySelective(box);
    }

    @Override
    public int deleteBox(Integer id) {
        String boxNum = boxMapper.selectByPrimaryKey(id).getBoxNum();
        String deviceSQL = " SELECT * FROM device WHERE box_num ='" + boxNum + "'";
        List<PageData> devices = regionMapper.selectListSQL(deviceSQL);

        if (devices == null || devices.size() == 0) {
            return boxMapper.deleteByPrimaryKey(id);
        }
        return -1;
    }

    @Override
    public List getSimpleBox(String projectNum) {
        String sql = "SELECT id,box_num name,town FROM box WHERE project_num='" + projectNum + "'";
        return regionMapper.selectListSQL(sql);
    }

    @Override
    public Integer deleteProjectMaterial(BoxMaterial boxMaterial) {
        return boxMaterialMapper.delete(boxMaterial);
    }

    @Override
    public int addProjectMaterial(BoxMaterial boxMaterial) {
        return boxMaterialMapper.insertSelective(boxMaterial);
    }

    @Override
    public List<BoxMaterial> getProjectMaterial(BoxMaterial boxMaterial) {
        return boxMaterialMapper.select(boxMaterial);
    }
}
